package cookieAndSession;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author： pang-yun
 * @date： 2021-11-29 11:49
 * 功能目的 ：用于 cookie 相关操作
 */

public class MyCookie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.创建cookie
        Cookie cookie = new Cookie("name","pang");
        Cookie age = new Cookie("age", "18");
        // 2. 通知客户端 保存 cookie
        resp.addCookie(cookie);
        resp.addCookie(age);
        // utf-8 编码
        resp.setContentType("text/html; charset=utf-8");

        // 得到 所有的cookie
        Cookie[] cookies = req.getCookies();

        Arrays.stream(req.getCookies()).forEach(a-> {
            try {
                resp.getWriter().write(a.getName()+ " "+ a.getValue()+"||||||||||||||||||     ");
                System.out.println();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}
